#pragma once
#include "GRTXObj.h"

// 最基本的单位
class GRTXTriangle :public GRTXObject
{
private:
	// In world
	GRiVertex V0;
	GRiVertex V1;
	GRiVertex V2;

	// V1-V0
	GMath::Vector3 E1;
	// V2-V0
	GMath::Vector3 E2;

	GUSPtr<GRiMaterial> pMat;

	// 光源的判定
	GMath::Vector3 Emission;
public:
	GRTXTriangle(const GRiVertex& v0,
		const GRiVertex& v1,
		const GRiVertex& v2,
		GUSPtr<GRiMaterial> mat,
		GMath::Vector3 emission = GMath::Vector3(0.0f))
		: V0(v0), V1(v1), V2(v2),
		Emission(emission),
		pMat(mat)
	{

		E1 = v1.Position - v0.Position;
		E2 = v2.Position - v0.Position;

		/////////////
		/// 父属性
		////////////
		Area = (E1.Cross(E2)).Length() / 2.0f;

		Box.InitFromPoints(v0.Position, v1.Position);
		Box = Box.Union(v2.Position);
	};

	virtual bool IsLight() const override;


	virtual void SampleLight(GRiVertex& samplePos, float& pdf)const override;

public:
	virtual HitInfo Interset(const GRTXRay& ray) const override;

private:
	bool IsInside(const GRTXRay& ray, float& t, float& u, float& v) const;

	GRiVertex Interpolation(float u, float v)const;
};

